# Maintainer: Christoph Reiter <reiter.christoph@gmail.com>

_realname=itstool
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.0.6
pkgrel=3
arch=('any')
pkgdesc="XML to PO and back again (mingw-w64)"
url="http://itstool.org/"
license=("GPL3")
depends=("${MINGW_PACKAGE_PREFIX}-python3"
         "${MINGW_PACKAGE_PREFIX}-libxml2")
makedepends=("${MINGW_PACKAGE_PREFIX}-python3-setuptools")
source=(http://files.itstool.org/itstool/${_realname}-${pkgver}.tar.bz2
        'reloc.patch'
        'pyscript2exe.py')
sha256sums=('6233cc22726a9a5a83664bf67d1af79549a298c23185d926c3677afa917b92a9'
            'f292bcd116da4ea36ee9c42e206dbd9151d147da1ef5a078e743fc9ed0a4ab47'
            '00e03a8b9d45e620c6fabbf96f5ccb41d1e49cc9b35ccb846d423f235ad5bec6')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  patch -p1 -i ${srcdir}/reloc.patch

  autoreconf -fvi
}

build() {
  mkdir -p "${srcdir}/build-${CHOST}"
  cd "${srcdir}/build-${CHOST}"

  PYTHON="${MINGW_PREFIX}/bin/python3" \
    ../${_realname}-${pkgver}/configure \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --build=${MINGW_CHOST} \
    --prefix=${MINGW_PREFIX}

  make
}

check() {
  cd build-${CHOST}

  make check
}

package() {
  install=${_realname}-${CARCH}.install
  cd "${srcdir}/build-${CHOST}"

  make DESTDIR="${pkgdir}" install

  ${MINGW_PREFIX}/bin/python3 "${srcdir}/pyscript2exe.py" "${pkgdir}${MINGW_PREFIX}/bin/itstool"
  local _PRE_WIN="$(cygpath -m ${MINGW_PREFIX})"
  sed -e "s|${_PRE_WIN}|${MINGW_PREFIX}|g" \
    -i ${pkgdir}${MINGW_PREFIX}/bin/itstool-script.py
}
